package com.dlc.commonlibrary.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.dlc.commonlibrary.R;

/**
 * Created by kawaimisashi on 2017/7/18.
 */

public class MyItemView extends LinearLayout {

    public TextView txtLeft,txtRight;
    ImageView rightImage;
    private int mHeight;

    public static final int TXTLEFT = R.id.txt_left;
    public static final int TXTRIGHT = R.id.txt_right;
    public static final int RIGHTIMAGE = R.id.right_img;

    public MyItemView(Context context) {
        super(context);
        init(context,null);
    }

    public MyItemView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context,attrs);
    }

    public MyItemView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context,attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int specMode = MeasureSpec.getMode(widthMeasureSpec);
        heightMeasureSpec = MeasureSpec.makeMeasureSpec(mHeight,specMode);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    }

    private void init(Context context, AttributeSet attrs){
       /* mHeight = 95;*/
        mHeight=getResources().getDimensionPixelSize(R.dimen.my_item_height);
        View view = LayoutInflater.from(context).inflate(R.layout.view_my_item,this,true);

        txtLeft = (TextView) view.findViewById(TXTLEFT);
        txtRight = (TextView) view.findViewById(TXTRIGHT);
        rightImage = (ImageView) view.findViewById(RIGHTIMAGE);

        if (attrs != null) {
            TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyItemView);

            String leftText = typedArray.getString(R.styleable.MyItemView_leftText);

            this.txtLeft.setText(leftText);

            // 右侧按钮
            int rightDrawableRes = typedArray.getResourceId(R.styleable.MyItemView_rightDrawable, 0);

            if (rightDrawableRes != 0) {
                rightImage.setImageResource(rightDrawableRes);
            }

            boolean showRightImage =
                    typedArray.getBoolean(R.styleable.MyItemView_showRightImage, rightDrawableRes != 0);

            rightImage.setVisibility(showRightImage ? VISIBLE : GONE);
            // 右侧文字
            String rightString = typedArray.getString(R.styleable.MyItemView_rightText);
            txtRight.setText(rightString);

            boolean showRightText = typedArray.getBoolean(R.styleable.MyItemView_showRightText,
                    !TextUtils.isEmpty(rightString));
            txtRight.setVisibility(showRightText ? VISIBLE : GONE);
            typedArray.recycle();
        }


    }



}
